package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.RepairMaint;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.RepairMaintService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("sys/repairMaint")
@Api(tags = "维修维护管理")
public class RepairMaintController {


    @Autowired
    private RepairMaintService repairMaintService;


    @ApiOperation(value = "分页查询维修维护")
    @GetMapping
    public JsonResult<Page<RepairMaint>> findByPage(Page<RepairMaint> page, RepairMaint repairMaint) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Page<RepairMaint> repairMaintPage = repairMaintService.findByPage(page, repairMaint, shopId);
        return JsonResult.ok(repairMaintPage);
    }

    @ApiOperation(value = "根据ID查询维修维护")
    @GetMapping("{id}")
    public JsonResult<RepairMaint> findRepairMaint(@PathVariable Long id) {
        RepairMaint repairMaintPage = repairMaintService.getById(id);
        return JsonResult.ok(repairMaintPage);
    }


    @ApiOperation(value = "添加维修维护")
    @PostMapping
    public JsonResult addRepairMaint(@RequestBody RepairMaint repairMaint) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        repairMaint.setShopId(shopId);
        boolean save = repairMaintService.addRepairMaint(repairMaint);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改维修维护")
    @PutMapping
    public JsonResult updateRepairMaint(@RequestBody RepairMaint repairMaint) {
        boolean save = repairMaintService.updateRepairMaint(repairMaint);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置维修维护状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody RepairMaint repairMaint) {
        boolean save = repairMaintService.updateRepairMaint(repairMaint);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除维修维护")
    @DeleteMapping("{id}")
    public JsonResult<RepairMaint> findRepairMaint(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = repairMaintService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


}
